/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.utility;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import plagiadetector.datatype.PANCorpus;
import plagiadetector.detector.OberDetector;
import weka.core.tokenizers.NGramTokenizer;

/**
 * This class is used for parameter analysis.
 * @author Dell
 */
public class ARFFGenerator {
        public static ArrayList<PANCorpus> globalCorpus;
        public static NGramTokenizer NGT;
        
    public static void initiate(){
        globalCorpus = new ArrayList<PANCorpus>();
        NGT = new NGramTokenizer();
    }
    
    /**
     * Get all PAN Corpus XML from a file or directory.
     * @param addr file or directory address.
     */
    public static void getAllXML(String addr){
        initiate();
        File f = new File(addr);
        
        if(f.isFile()){
            processSingleXML(addr);
        }else{
            processDirectory(addr);
        }
        
        System.out.println("Number of processed XML(s) : "+globalCorpus.size());
    }
    
    public static void processSingleXML(String addr){
        globalCorpus.add(PANXMLParser.parseXML(addr));
    }
     
    public static void processDirectory(String addr){
        ArrayList<String> xmls = FileUtils.getFileNames(addr, "*.xml");        
        ArrayList<String> dirs = FileUtils.getSubDirectories(addr);
        
        for(String s : xmls){
            processSingleXML(addr+"\\"+s);
        }
       for(String s : dirs){
           processDirectory(addr+"\\"+s);
       }
    }
    
    
    public static int getNumberOfWords(String s){
        int words = 0;
        NGT.tokenize(s);
        
        while(NGT.hasMoreElements()){
            NGT.nextElement();
            words++;
        }
        
        return words;
    }
    
    public static void main(String[] args){
        getAllXML("D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\PAN-PC-2011\\intrinsic-detection-corpus\\suspicious-document");
        FileWriter fw;
        OberDetector od = new OberDetector();
        
        try {
            fw = new FileWriter("corpusdata.arff");
            fw.write("@relation isPlagiarised\n\n");
            
            fw.write("@attribute corpusnumber numeric\n");
            fw.write("@attribute corpuslength numeric\n");
            fw.write("@attribute corpuscases numeric\n");
            fw.write("@attribute corpuswords numeric\n");            
            fw.write("@attribute preprocessedcorpuswords numeric\n");            
            fw.write("@attribute class {true,false}\n\n");
            
            
            fw.write("@data\n\n");
            
            for(PANCorpus panc : globalCorpus){
                fw.write(panc.address.replace("D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\PAN-PC-2011\\intrinsic-detection-corpus\\suspicious-document\\part", "").substring(1).replaceAll("[^\\d]", "")+","+panc.getCorpusText().length()+","+panc.getNumPANCase()+","+getNumberOfWords(panc.getCorpusText())+","+getNumberOfWords(od.preprocessText(panc.getCorpusText()))+","+(panc.getNumPANCase() > 0 ? "true" : false)+"\n");
            }
            
            fw.close();
        } catch (IOException ex) {
            Logger.getLogger(ARFFGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }
}
